package com.pubinfo.passbook.engine.process.variable.medicine;

import com.pubinfo.passbook.common.entity.TEnginePlaceholderConst;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.constant.StaffStatusEnum;
import com.pubinfo.passbook.common.model.dto.witeoff.MedicineOnJobOrRetireDTO;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.common.service.engine.EngineComputePlaceHolderConstService;
import com.pubinfo.passbook.common.service.ruler.InformationService;
import com.pubinfo.passbook.engine.utils.ApplicationContextUilts;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.List;

/**
 * 泰顺 最大一档报销金额
 * <p>
 * 默认情况, 该参数处理器会优先读取常量占位符表中名**包含**:
 * ["温州市泰顺最大一档在职报销金额" "温州市泰顺最大一档退休报销金额" ] 名称的常量占位符来返回不同值.
 * 如果不存在该些常量, 则默认使用该类中默认的值. 分别为 在职金额3500, 退休金额4000
 *
 * @author zhouch
 * @date 2022/2/16 9:23
 */
@Slf4j
public class TaiShunMaximumAmountProcessor extends MedicineVariableProcessor {

    // 温州市泰顺最大一档在职报销金额
    private static final BigDecimal JOB = new BigDecimal(3500);

    // 温州市泰顺最大一档退休报销金额
    private static final BigDecimal RETIRE = new BigDecimal(4000);

    private static final String JOB_CONST_KEYWORD = "温州市泰顺最大一档在职报销金额";

    private static final String RETIRE_CONST_KEYWORD = "温州市泰顺最大一档退休报销金额";


    EngineComputePlaceHolderConstService constService;

    DubboService dubboService;

    public TaiShunMaximumAmountProcessor() {
        constService = ApplicationContextUilts.getBean(EngineComputePlaceHolderConstService.class);
        dubboService = ApplicationContextUilts.getBean(DubboService.class);
    }

    @Override
    public BigDecimal get(MedicineOnJobOrRetireDTO data) {

        List<PayInformation> payInformations = cache.getPayInformationData();

        PayInformation payInformat = payInformations.get(0);
        String idNo = payInformat.getPayerPartyCode(); //报账人的身份证号
        String statusName = dubboService.getPersonsDetailByStatus(idNo);
        Integer status = StaffStatusEnum.getEnumByStatus(statusName).getCode();

        if (status.equals(1)) {
            List<TEnginePlaceholderConst> constants = constService.likeName(JOB_CONST_KEYWORD);
            if (!constants.isEmpty()) {
                if (constants.size() > 1) {
                    log.warn("[泰顺最大一档报销金额参数处理器]: 查询到有多个'{}'常量占位符, 注意检查计算是否错误."
                            , JOB_CONST_KEYWORD);
                }
                // 有多个的话, 默认取查询到的第一个
                return constants.get(0).getValue();
            }

            return JOB;
        } else if (status.equals(0) || status.equals(2)) {

            List<TEnginePlaceholderConst> constants = constService.likeName(RETIRE_CONST_KEYWORD);
            if (!constants.isEmpty()) {
                if (constants.size() > 1) {
                    log.warn("[泰顺最大一档报销金额参数处理器]: 查询到有多个'{}'常量占位符, 注意检查计算是否错误."
                            , RETIRE_CONST_KEYWORD);
                }
                // 有多个的话, 默认取查询到的第一个
                return constants.get(0).getValue();
            }

            return RETIRE;
        }

        throw new ServiceException("泰顺最大一档报销金额参数处理器：status不属于在职，退休，内退。");
    }
}
